/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.converter.TrainFlowHandleConverter;
import com.xy.biz.ts.domain.query.TrainFlowHandleQuery;
import com.xy.biz.ts.domain.req.*;
import com.xy.biz.ts.domain.resp.GetTrainResponse;
import com.xy.biz.ts.domain.resp.GetTrainSumResponse;
import com.xy.lang.domain.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.query.TrainSumQuery;
import com.xy.biz.ts.domain.converter.TrainSumConverter;
import com.xy.biz.ts.service.TrainSumService;

/**
 * @author zgy
 * @date 2021-7-29
 */
@Api(tags = "培训总结")
@Slf4j
@RequestMapping("/api/ts/train_sum")
@RestController
public class TrainSumController {

    @Autowired
    private TrainSumService trainSumService;

    @ApiOperation("开始培训总结流程")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveTrainSum(@RequestBody SaveTrainSumRequest request) {
        return trainSumService.saveTrainSum(TrainSumConverter.toBO(request));
    }

    @Validatable
    @ApiOperation("培训总结流程部门主管审核")
    @PostMapping(value = "/admin_audit")
    public Result<?> adminAudit(@RequestBody HandleTrainSumFlowRequest request) {
        return trainSumService.adminAudit(request);
    }

    @Validatable
    @ApiOperation("培训总结流程人事行政部意见")
    @PostMapping(value = "/opinion")
    public Result<?> opinion(@RequestBody HandleTrainSumReleaseFlowRequest request) {
        return trainSumService.opinion(request);
    }

    @Validatable
    @ApiOperation("培训总结流程-待办事项")
    @PostMapping(value = "/page_todo")
    public Result<Page<GetTrainSumResponse>> pageTodoTrainFlow(@RequestBody PageRequest request) {
        TrainFlowHandleQuery trainFlowHandleQuery = TrainFlowHandleConverter.toQuery(request);
        return Result.success(trainSumService.searchTrainSumFlowTodo(trainFlowHandleQuery));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllTrainSum(@RequestBody Collection<SaveTrainSumRequest> collection) {
        trainSumService.saveAllTrainSum(TrainSumConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateTrainSum(@RequestBody SaveTrainSumRequest request) {
        trainSumService.updateTrainSum(TrainSumConverter.toBO(request));
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteTrainSum(@RequestBody GetTrainSumRequest request) {
        trainSumService.deleteTrainSum(request.getTrainSumId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    public Result<GetTrainSumResponse> getTrainSum(@RequestBody GetTrainSumRequest request) {
        return Result.success(TrainSumConverter.toResponse(trainSumService.getTrainSum(request.getTrainSumId())));
    }

    @PostMapping(value = "/list_by_ids")
    public Result<List<GetTrainSumResponse>> listTrainSumById(@RequestBody Collection<Long> collection) {
        return Result.success(TrainSumConverter.toResponse(trainSumService.listTrainSumById(collection)));
    }

    @PostMapping(value = "/list")
    public Result<List<GetTrainSumResponse>> listTrainSum(@RequestBody ListTrainSumRequest request) {
        return Result.success(TrainSumConverter.toResponse(trainSumService.listTrainSum(TrainSumConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    public Result<Integer> countTrainSum(@RequestBody ListTrainSumRequest request) {
        return Result.success(trainSumService.countTrainSum(TrainSumConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    public Result<Page<GetTrainSumResponse>> pageTrainSum(@RequestBody ListTrainSumRequest request) {
        TrainSumQuery query = TrainSumConverter.toQuery(request);
        List<GetTrainSumResponse> list = TrainSumConverter.toResponse(trainSumService.listTrainSum(query));
        int count = trainSumService.countTrainSum(query);
        return Result.success(new Page<>(list, count));
    }

}
